package fr.univ.lille1.android.fragments;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ItemClick;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.UiThread;

import fr.univ.lille1.android.R;
import fr.univ.lille1.android.adapters.CommentAdapter;
import fr.univ.lille1.android.domain.Message;
import fr.univ.lille1.android.network.MessageMockManager;
import fr.univ.lille1.android.network.RequestFactory;

@EFragment
@OptionsMenu(R.menu.menu_list_comments)
public class ListCommentsFragment extends SherlockListFragment {
	private MessageMockManager mManager;
	private View mCurrentView;
	private OnNavigationCommentSelected mListener;

	@Bean
	CommentAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_list_comments, container,
				false);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mListener = (OnNavigationCommentSelected) activity;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mManager = new MessageMockManager(RequestFactory.getInstance()
				.getRestAdapter());
		getListView().setAdapter(mAdapter);
	}

	@Override
	public void onResume() {
		super.onResume();
		getCommentsOnServer();
	}

	@Background
	void getCommentsOnServer() {
		// TODO Here, we call API from the other maven projects to retrieve
		// comments of the current user's position.
		onPostExecute(mManager.findByLocation());
	}

	@UiThread
	void onPostExecute(List<Message> result) {
		if (result == null || result.isEmpty()) {
			return;
		}
		mAdapter.addAll(result);
		mAdapter.notifyDataSetChanged();
	}

	@OptionsItem(R.id.menu_add_comment)
	void menuAddComment() {
		if (mListener != null) {
			mListener.changeToAddComment();
		}
	}

	@ItemClick
	void listItemClicked(int pos) {
		mCurrentView = getListView().getChildAt(pos);
		mCurrentView.setBackgroundResource(R.color.holo_blue_light);
		getSherlockActivity().startActionMode(mActionMode);
	}

	private final ActionMode.Callback mActionMode = new Callback() {
		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			if (mCurrentView != null) {
				mCurrentView.setBackgroundResource(R.color.background_light);
				mCurrentView = null;
			}
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.menu_item_comment, menu);
			return true;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			// TODO Here, we call API from the other maven projects for like,
			// dislike and report a comment.
			switch (item.getItemId()) {
			case R.id.menu_like:
				break;
			case R.id.menu_unlike:
				break;
			case R.id.menu_report:
				break;
			default:
				return false;
			}
			mode.finish();
			return true;
		}
	};

	public interface OnNavigationCommentSelected {
		void changeToAddComment();
	}
}
